﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for clsKetQuaBaithi
/// </summary>
public class clsKetQuaBaithi
{
    DataClassesDataContext kqt = new DataClassesDataContext();
	public clsKetQuaBaithi()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string tenbttrave(string madt)
    {
        var tenbt = (from p in kqt.tbl_DETHIs
                     where p.MaDT == madt
                     select p.TenDeThi).FirstOrDefault().ToString();
        return tenbt;
    }
    public void ThemMaBTvsTgian(string mabt, int thoigian,string madt,string masv)
    {
        var tenbt = (from p in kqt.tbl_DETHIs
                     where p.MaDT == madt
                     select p.TenDeThi).FirstOrDefault();

        tbl_BAITHI baithi = new tbl_BAITHI();
        
        baithi.MaBT = mabt;
        baithi.MaDT = madt;
        baithi.TenBaiThi = tenbt;
        DateTime dt = DateTime.Now;
        baithi.NgayThi = dt.AddSeconds(thoigian);
        baithi.MsSinhVien = masv;
        kqt.tbl_BAITHIs.InsertOnSubmit(baithi);
        kqt.SubmitChanges();
        //kqt.sp_currentNgaygio(thoigian);
        
        

 
    }
    public int truTime(string mabt)
    {
        DateTime endtime = (from p in kqt.tbl_BAITHIs
                               where p.MaBT == mabt
                               select p.NgayThi).FirstOrDefault();
        TimeSpan gioketthuc = endtime.TimeOfDay;
        TimeSpan giohientai = DateTime.Now.TimeOfDay;
        TimeSpan tongtg = gioketthuc.Subtract(giohientai);
        int seconds = tongtg.Hours * 3600 + tongtg.Minutes * 60 + tongtg.Seconds;
        return seconds;
    }
    //public void ThemBaiThi(string mabt, string ngaythi, string tenbt)
    //{
        
    //    tbl_BAITHI baithi = new tbl_BAITHI();
    //    //DateTime dt = new DateTime();
        
    //    baithi.MaDT = madt;
    //    //baithi.NgayThi = ngaythi;
    //    baithi.TenBaiThi = tenbt;
    //    baithi.MsSinhVien= masv;
    //    //baithi.SoCauDung = socaudung;
    //    //baithi.Diem = diem;

    //    kqt.tbl_BAITHIs.InsertOnSubmit(baithi);
    //    kqt.SubmitChanges();
                            
                      
    //}
    public void KetQua(string mabt,string madt,string tengbt)
    {
        var listcaudung = from p in kqt.tbl_NGANHANGCAUHOIs
                          from q in kqt.tbl_CHITIETBAITHIs
                          where p.DADUNG == q.Traloi && q.MaBT==mabt
                          select p.DADUNG;
        int socaudung = listcaudung.Count();
        int socauhoi = (from p in kqt.tbl_DETHIs
                        from q in kqt.tbl_QUYTACDETHIs
                        where q.MaQT == p.MaQT && p.MaDT == madt
                        select q.SoCH).Single();
        double diem = 10 * socaudung / (double)socauhoi;

        var kqthi = (from p in kqt.tbl_BAITHIs
                     where p.MaBT == mabt
                     select p).Single();
        kqthi.TenBaiThi = tengbt;
        kqthi.Socaudung = socaudung;
        kqthi.Diem = Math.Round(diem, 2, MidpointRounding.AwayFromZero);
        kqt.SubmitChanges();
 
    }

}
